/**
 * @file tiny_semaphore.c
 * @brief 信号量实现
 * @author 张恒宣 
 * @LastEditTime 2024/9/22
 * 
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*********************
 *      头文件
 *********************/

#include "tiny_semaphore.h"
#include "tiny_thread.h"
#include "tiny_rtos_context.h"
#include "tiny_malloc.h"

/*********************
*      公共函数 
*********************/

/// @brief 创建信号量
/// @param init_val 初始值
/// @param max_val 最大值
/// @return 信号量实例, 如果失败返回tiny_null
tiny_semaphore_t tiny_semaphore_create(tiny_int32_t init_val, tiny_int32_t max_val)
{
	tiny_semaphore_t semaphore = tiny_malloc(sizeof(struct tiny_semaphore));
	if (!semaphore)
	{
		return tiny_null;
	}

	tiny_init_list(&semaphore->receiver_list);
	semaphore->val = init_val;
	semaphore->max_val = max_val;
	return semaphore;
}

/// @brief 创建二进制信号量
/// @param  无需
/// @return  信号量实例, 如果失败返回tiny_null
tiny_semaphore_t tiny_binary_semaphore_create(void)
{
	return tiny_semaphore_create(1, 1);
}

/// @brief 释放信号量
/// @param semaphore 待释放的信号量
void tiny_semaphore_release(tiny_semaphore_t semaphore)
{
	if (semaphore)
	{
		tiny_wakeup_all_list_thread(&semaphore->receiver_list);
		tiny_free(semaphore);
	}
}

/// @brief 获得信号量
/// @param semaphore 待获取的信号量实例
/// @param timeout 超时时间 小于于0表示无限等待
/// @return tiny_true表示成功 tiny_false表示失败
tiny_ret_status_t tiny_semaphore_take(tiny_semaphore_t semaphore, tiny_int32_t timeout)
{

	if (!semaphore)
	{
		return tiny_false;
	}

	while (1)
	{
		int flag = disable_interrupt();

		if (semaphore->val > 0)
		{
			semaphore->val--;
			enable_interrupt(flag);
			return tiny_true;
		}

		if (timeout == 0)
		{
			enable_interrupt(flag);
			return tiny_false;
		}

		if (0 == tiny_suspend_thread_in_dst_list(get_self_thread(), &semaphore->receiver_list))
		{
			enable_interrupt(flag);
			return tiny_false;
		}

		tiny_thread_t *cur_running_thread = get_self_thread();
		cur_running_thread->timer_ret = THREAD_BLOCK_NOT_TIMEOUT;
		if (timeout > 0)
		{
			tiny_timer_set_interval(&cur_running_thread->timer, (tiny_uint32_t)CONVERT_MS_TO_TICK(timeout));
			tiny_timer_start(&cur_running_thread->timer);
			cur_running_thread->statu = THREAD_BLOCKED;
		}

		enable_interrupt(flag);

		schedule();

		if (cur_running_thread->timer_ret == THREAD_BLOCK_TIMEOUT)
		{
			return tiny_false;
		}
	}

}

/// @brief 释放信号量
/// @param semaphore 待释放的信号量
/// @return tiny_true 成功 tiny_false 失败
tiny_ret_status_t tiny_semaphore_give(tiny_semaphore_t semaphore)
{
	tiny_ret_status_t ret = tiny_false;
	if (!semaphore)
	{
		return ret;
	}

	int flag = disable_interrupt();

	if (semaphore->max_val < 0 || semaphore->val < semaphore->max_val)
	{
		semaphore->val++;
		ret = tiny_true;
	}

	tiny_wakeap_one_list_thread(&semaphore->receiver_list, tiny_true);

	enable_interrupt(flag);

	return ret;
}
